package com.bzr.emos.wx.controller;

import com.bzr.emos.wx.Service.UserService;
import com.bzr.emos.wx.common.util.R;
import com.bzr.emos.wx.controller.from.TestSayHelloFrom;
import com.bzr.emos.wx.db.dao.UserDao;
import com.bzr.emos.wx.db.pojo.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api")
@Tag(name = "body参数")
@Slf4j

public class BodyController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;



    @Operation(summary = "普通body请求")
    @PostMapping("/body")
    public R body(@RequestBody TestSayHelloFrom fileResp){
        return R.ok(fileResp.getName());
    }

    @Operation(summary = "普通body请求+Param+Header+Path")
    @Parameters({
            @Parameter(name = "id",description = "文件id",in = ParameterIn.PATH),
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
            @Parameter(name = "name",description = "文件名称",required = true,in=ParameterIn.QUERY)
    })
    @PostMapping("/bodyParamHeaderPath/{id}")
    public R  bodyParamHeaderPath(@PathVariable("id") String id,
                                  @RequestHeader("token") String token,
                                  @RequestParam("name")String name){

        return R.ok().put("id", id).put("token", token).put("name", name);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @Operation(summary = "测试请求创建用户")
    @PostMapping("/add")
    public int add(@RequestBody User user){  //
        log.info(user.toString());

        return 1;

    }

    @PreAuthorize("hasRole('ADMIN')")
    @Operation(summary = "测试查看用户列表")
    @PostMapping("/list")
    public R list(){  //
        List<User> users = userDao.selectList(null);

        if (users.size() > 0){
            return R.ok().put("users", users);
        }else {
            return R.error("未找到用户信息");
        }


    }


}

